﻿package UIComponents// ActionScript file
{
	import Classes.staticinfo.Language;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	public class FrameBuildingItem extends Sprite
	{
		private var _buildingItem:IUseItem;
		private var _shopItem:IGD_ShopItem;
		
		public var _bmb:DisplayObject;
		
		public function FrameBuildingItem():void
		{
			this.init();
		}
		
		private function init():void
		{
			this.addListener();
			this._bmb=this.getChildByName("bmb");
			this.getChildByName("hotFlag").visible=false;
			this.getChildByName("newFlage").visible=false;
			this.getChildByName("notLv").visible=false;
			this.getChildByName("haveOne").visible	=false;
			this.showPrice(false);
		}
		
		private function addListener():void
		{
			this.addEventListener(MouseEvent.ROLL_OVER,overHandler);
			this.addEventListener(MouseEvent.ROLL_OUT,outHandler);
			this.addEventListener(MouseEvent.CLICK,clickHandler);
		}
		
		private function overHandler(e:MouseEvent):void
		{
			
		}
		
		private function outHandler(e:MouseEvent):void
		{
			
		}
		
		private function clickHandler(e:MouseEvent):void
		{
			var isClickItem:Boolean = false;
			
			if(this.buildingItem && this.buildingItem.useEnabled){this.buildingItem.useItem();isClickItem=true;}
			if(this._shopItem && !this.buildingItem  && this._shopItem.isReachLv)
			{
				isClickItem		=		true;
				this._shopItem.istore.doSomeThingFun(this._shopItem.shopItemType,this._shopItem.price,this._shopItem.moneyType,this._shopItem.isReachMoney);
			}
			
			if(isClickItem)this.dispatchEvent(new PanelEvent("isClickItem",true));	//点击到有效项
		}
		
		/**
		 * 设定仓库建筑道具 
		 * 
		 */		
		public function setBuildingItem(buildingItem:IUseItem):void
		{
			this.reset();
			this.showPrice(false);
			this._buildingItem=buildingItem;
			if(this.buildingItem)
			{
				var tips:String=buildingItem.itemName+"："+buildingItem.description;
				this.setItemPic(this.buildingItem.itemPic,tips);
			}
			
			if(this.buildingItem!=null && this.buildingItem.useEnabled)this.buttonMode	=	true;
			else	this.buttonMode		=	false;
		}
		
		/**
		 * 设定装饰跟建筑 
		 * @param item
		 * 
		 */		
		public function setBuildingItemLinkShop(item:IGD_ShopItem):void
		{
			this.reset();
			this._shopItem=item;
			if(this._shopItem)
			{
				this.showPrice(true);
				this.setItemPrice(item.price);
				var tips1:String=item.itemName+"："+item.itemDes;
				this.setItemPic(item.shopItemPic,tips1);
//				var isObPic:Boolean=item.moneyType==0?true:false;
				this.setItemPriceType(item.moneyType);
				this.controlFlag(item.shopItemState);
				if(item.isReachLv)
				{
					this.getChildByName("notLv").visible=false;
				}else{
					this.getChildByName("notLv").visible=true;
					((this.getChildByName("notLv") as DisplayObjectContainer).getChildByName("needLv") as TextField).text=Language.Lv+item.needLever;
				}
			}
		}
		
		
		public function setItemPic(value:DisplayObject,tips:String):void
		{
			
			((this.getChildByName("frame_icon") as Sprite).getChildByName("iconContainer") as IconContainer).addPic(value,tips);
		}
		
		public function setItemPriceType(moneyType:int=1):void
		{
			switch(moneyType)
			{
				case 0://ob
					this.getChildByName("icon_jb").visible=false;
					this.getChildByName("exchandIcon").visible=false;
					this.getChildByName("icon_obi").visible=true;
					break;
				case 1://jb
					this.getChildByName("icon_jb").visible=true;
					this.getChildByName("exchandIcon").visible=false;
					this.getChildByName("icon_obi").visible=false;
					break;
				case 2://item
					this.getChildByName("icon_jb").visible=false;
					this.getChildByName("exchandIcon").visible=true;
					this.getChildByName("icon_obi").visible=false;
					break;
				default:
					this.getChildByName("icon_jb").visible=false;
					this.getChildByName("exchandIcon").visible=true;
					this.getChildByName("icon_obi").visible=false;
					break;
			}
		}
		
		public function setItemPrice(value:Number):void
		{
			(this.getChildByName("txt_price") as TextField).text=String(value);
		}
		
		public function showPrice(value:Boolean):void
		{
			this.getChildByName("txt_price").visible=value;
			this.getChildByName("icon_jb").visible=value;
			this.getChildByName("exchandIcon").visible=value;
			this.getChildByName("icon_obi").visible=value;
		}
		
		public function reset():void
		{
			this.setItemPic(null,"");
			this._bmb.visible=false;
			this.showPrice(false);
			this._buildingItem=null;
			this._shopItem=null;
			this.getChildByName("hotFlag").visible=false;
			this.getChildByName("newFlage").visible=false;
			this.getChildByName("notLv").visible=false;
		}
		
		/**
		 *  控制标签的显示
		 *   0下架  1上架  2热卖  3新品
		 * @return
		 */
		public function controlFlag(flag:Number):void
		{
			switch (flag)
			{
				case 2:
					this.getChildByName("hotFlag").visible=true;
					this.getChildByName("newFlage").visible=false;
					break;
				case 3:
					this.getChildByName("hotFlag").visible=false;
					this.getChildByName("newFlage").visible=true;
					break;
				default:
					this.getChildByName("hotFlag").visible=false;
					this.getChildByName("newFlage").visible=false;
					break;
			}
		}
		
		public function release():void
		{
			this._buildingItem=null;
			while(this.parent)this.parent.removeChild(this);
		}

		/*getter & setter*/
		public function get buildingItem():IUseItem
		{
			return _buildingItem;
		}
		
		/**
		 *是否显示已建造 
		 * @param value
		 * 
		 */
		public function setHaveOne(value:Boolean):void
		{
			this.getChildByName("haveOne").visible		=	value;
			if(!value){
				this.addEventListener(MouseEvent.CLICK,clickHandler);
			}
			else{
				if( this.hasEventListener(MouseEvent.CLICK))
				this.removeEventListener(MouseEvent.CLICK,clickHandler);
			}
		}

	}
}